package com.rectcircle.scalatrateset.db

import org.scalatra.ScalatraBase
import org.squeryl.{Session, SessionFactory}

/**
  * @author Rectcircle
  *         createTime 2017/1/7
  */
object DatabaseSessionSupport {
	val key = {
		val n = getClass.getName
		if (n.endsWith("$")) n.dropRight(1) else n
	}
}


trait DatabaseSessionSupport { this:ScalatraBase =>
	import DatabaseSessionSupport._
	
	def dbSession = request.get(key).orNull.asInstanceOf[Session]
	
	before() {
		request(key) = SessionFactory.newSession
		dbSession.bindToCurrentThread
	}
	
	after() {
		dbSession.close
		dbSession.unbindFromCurrentThread
	}
	
	
}
